package cn.newtouch.controller;

import cn.newtouch.block.UserBlock;
import cn.newtouch.domain.User;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RefreshScope //动态刷新配置文件的一个注解
@RestController
@RequestMapping("/user")
public class UserContorller {

    @Value("${server.port}")
    private Long port;

    @GetMapping("/getById/{id}")
    @SentinelResource(value = "user-getById",blockHandler = "BlockHandlerMethod",blockHandlerClass = UserBlock.class)
   // @SentinelResource(value = "user-getById",fallback = "UserFallBack")
    public User getById(@PathVariable("id") Long id){
        return new User(id,"三国演义","三英战吕布！！"+port);
    }


    // 限流与阻塞处理 : 参数要和 被降级的方法参数一样
    /*public User BlockHandlerMethod(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        System.out.println("限流了。。。。。。。");
        return new User(-1L,"触发限流","限流了！！");
    }*/

   /* public User UserFallBack(@PathVariable("id") Long id){
        return new User(-1L,"触发熔断","熔断了！！");
    }
*/

}
